Imports PTT.CarRes.Data

Public Class ClassQueryInvCarType
    Public Function Add(ByVal InvCarType As QueryInvCarType, ByVal StrConn As String) As Boolean
        Dim ClsInvCarType As New ClassInvCarType
        Dim Conn As OleDb.OleDbConnection
        Conn = ClsInvCarType.ConnectionOpen(StrConn)
        Dim ret As Boolean = False
        With ClsInvCarType
            .TypeID = InvCarType.TypeID
            .TypeName = InvCarType.TypeName
            .TypeStatus = InvCarType.TypeStatus
            .TCostDay = InvCarType.TCostDay
            .TCostHoliday = InvCarType.TCostHoliday
            .TOtday = InvCarType.TOtday
            .TOtHoliday = InvCarType.TOtHoliday
            .TCostDriver = InvCarType.TCostDriver
            ret = .USP_Save(Conn)
        End With
        ClsInvCarType.ConnectionClose(Conn)
        Return ret
    End Function

    Public Function Edit(ByVal InvCarType As QueryInvCarType, ByVal StrConn As String) As Boolean
        Dim ClsInvCarType As New ClassInvCarType
        Dim Conn As OleDb.OleDbConnection
        Conn = ClsInvCarType.ConnectionOpen(StrConn)
        Dim ret As Boolean = False
        With ClsInvCarType
            .TypeID = InvCarType.TypeID
            .Load(Conn)
            .TypeName = InvCarType.TypeName
            .TypeStatus = InvCarType.TypeStatus
            .TCostDay = InvCarType.TCostDay
            .TCostHoliday = InvCarType.TCostHoliday
            .TOtday = InvCarType.TOtday
            .TOtHoliday = InvCarType.TOtHoliday
            .TCostDriver = InvCarType.TCostDriver
            ret = .USP_Update(Conn)
        End With
        ClsInvCarType.ConnectionClose(Conn)
        Return ret
    End Function

    Public Function Delete(ByVal InvCarType As QueryInvCarType, ByVal StrConn As String) As Boolean
        Dim ClsInvCarType As New ClassInvCarType
        Dim Conn As OleDb.OleDbConnection
        Conn = ClsInvCarType.ConnectionOpen(StrConn)
        Dim ret As Boolean = False
        With ClsInvCarType
            .TypeID = InvCarType.TypeID
            .Load(Conn)
            ret = .USP_Delete(Conn)
        End With
        ClsInvCarType.ConnectionClose(Conn)
        Return ret
    End Function

    Public Function Search(Optional ByVal ParaTypeID As String = "", _
    Optional ByVal ParaTypeName As String = "", _
    Optional ByVal ParaTypeStatus As String = "", Optional ByVal StrConn As String = "") As QueryInvCarTypes
        Dim ClsInvCarType As New ClassInvCarType
        Dim Conn As OleDb.OleDbConnection
        Conn = ClsInvCarType.ConnectionOpen(StrConn)
        Dim ret As QueryInvCarTypes = Nothing
        Dim str As String = ""
        Dim Condition As String = ""
        str &= " Select * from InvCarType"
        If ParaTypeID & "" <> "" Then
            If Condition & "" <> "" Then
                Condition &= " and "
            End If
            Condition &= "(TypeID = " & ParaTypeID & ")"
        End If
        If ParaTypeName & "" <> "" Then
            If Condition & "" <> "" Then
                Condition &= " and "
            End If
            Condition &= "(TypeName like '%" & ParaTypeName & "%')"
        End If
        If ParaTypeStatus & "" <> "" Then
            If Condition & "" <> "" Then
                Condition &= " and "
            End If
            Condition &= "(TypeStatus like '%" & ParaTypeStatus & "%')"
        End If

        If Condition & "" <> "" Then
            str &= " where "
            str &= Condition
        End If
        Dim ds As DataSet
        With ClsInvCarType
            ds = .BindData(Conn, , , str)
            If Not ds Is Nothing Then
                Dim i As Integer
                Dim results As New QueryInvCarTypes
                Dim RTCostDay As Decimal = -1
                Dim RTCostHoliday As Decimal = -1
                Dim RTOtday As Decimal = -1
                Dim RTOtHoliday As Decimal = -1
                Dim RTCostDriver As Decimal = -1

                Try
                    For i = 0 To ds.Tables(0).Rows.Count - 1
                        With ds.Tables(0).Rows(i)
                            If IsNumeric(.Item("TCostDay")) Then
                                RTCostDay = .Item("TCostDay")
                            Else
                                RTCostDay = -1
                            End If
                            If IsNumeric(.Item("TCostHoliday")) Then
                                RTCostHoliday = .Item("TCostHoliday")
                            Else
                                RTCostHoliday = -1
                            End If
                            If IsNumeric(.Item("TOtday")) Then
                                RTOtday = .Item("TOtday")
                            Else
                                RTOtday = -1
                            End If
                            If IsNumeric(.Item("TOtHoliday")) Then
                                RTOtHoliday = .Item("TOtHoliday")
                            Else
                                RTOtHoliday = -1
                            End If
                            If IsNumeric(.Item("TCostDriver")) Then
                                RTCostDriver = .Item("TCostDriver")
                            Else
                                RTCostDriver = -1
                            End If

                            results.Add(New QueryInvCarType( _
                              .Item("TypeID") & "", _
                              .Item("TypeName") & "", _
                              .Item("TypeStatus") & "", _
                              RTCostDay, _
                              RTCostHoliday, _
                              RTOtday, _
                              RTOtHoliday, _
                              RTCostDriver _
                            ))
                        End With
                    Next
                    ret = results
                Catch ex As Exception
                    Return Nothing
                    'Throw New DALException(ex.Message)
                End Try
            End If
        End With
        ClsInvCarType.ConnectionClose(Conn)
        Return ret
    End Function
    Public Function Load(Optional ByVal ParaTypeID As String = "", Optional ByVal StrConn As String = "") As QueryInvCarTypes
        Dim ClsInvCarType As New ClassInvCarType
        Dim Conn As OleDb.OleDbConnection
        Conn = ClsInvCarType.ConnectionOpen(StrConn)
        Dim ret As QueryInvCarTypes = Nothing
        Dim str As String = ""
        Dim Condition As String = ""
        str &= " Select * from InvCarType"
        If ParaTypeID & "" <> "" Then
            If Condition & "" <> "" Then Condition &= " and "
            Condition &= "(TypeID = " & ParaTypeID & ")"
        End If

        If Condition & "" <> "" Then
            str &= " where "
            str &= Condition
        Else
            Return Nothing
            Exit Function
        End If
        Dim ds As DataSet
        With ClsInvCarType
            ds = .BindData(Conn, , , str)
            If Not ds Is Nothing Then
                Dim i As Integer
                Dim results As New QueryInvCarTypes
                Dim RTCostDay As Decimal = -1
                Dim RTCostHoliday As Decimal = -1
                Dim RTOtday As Decimal = -1
                Dim RTOtHoliday As Decimal = -1
                Dim RTCostDriver As Decimal = -1

                Try
                    For i = 0 To ds.Tables(0).Rows.Count - 1
                        With ds.Tables(0).Rows(i)
                            If IsNumeric(.Item("TCostDay")) Then
                                RTCostDay = .Item("TCostDay")
                            Else
                                RTCostDay = -1
                            End If
                            If IsNumeric(.Item("TCostHoliday")) Then
                                RTCostHoliday = .Item("TCostHoliday")
                            Else
                                RTCostHoliday = -1
                            End If
                            If IsNumeric(.Item("TOtday")) Then
                                RTOtday = .Item("TOtday")
                            Else
                                RTOtday = -1
                            End If
                            If IsNumeric(.Item("TOtHoliday")) Then
                                RTOtHoliday = .Item("TOtHoliday")
                            Else
                                RTOtHoliday = -1
                            End If
                            If IsNumeric(.Item("TCostDriver")) Then
                                RTCostDriver = .Item("TCostDriver")
                            Else
                                RTCostDriver = -1
                            End If

                            results.Add(New QueryInvCarType( _
                              .Item("TypeID") & "", _
                              .Item("TypeName") & "", _
                              .Item("TypeStatus") & "", _
                              RTCostDay, _
                              RTCostHoliday, _
                              RTOtday, _
                              RTOtHoliday, _
                              RTCostDriver _
                            ))
                        End With
                    Next
                    ret = results
                Catch ex As Exception
                    Return Nothing
                    'Throw New DALException(ex.Message)
                End Try
            End If
        End With
        ClsInvCarType.ConnectionClose(Conn)
        Return ret
    End Function
End Class
